//
//  GroupPurcaseView.swift
//  GhostWorld
//
//  Created by Kam on 2018/6/7.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit

protocol GroupPurchaseViewDelegate {
  func displayPurchaseInfo()
  func buySession()
}

class GroupPurcaseView: UICollectionReusableView {

  // MARK: - IBOutlets
  
  @IBOutlet private weak var priceLabel: UILabel!
  @IBOutlet private weak var purchasedLabel: UILabel!
  @IBOutlet private weak var purchaseView: UIView!
  
  // MARK: - Private vars
  
  private var isFinished = false
  
  // MARK: - Private methods
  
  private func setUI(purchased: Bool, allAlbumInGroupPurchased: Bool) {
    if purchased || allAlbumInGroupPurchased {
//      if isFinished {
        purchaseView.isHidden = true
        purchasedLabel.isHidden = false
//      }
//      else {
//        purchaseView.isHidden = false
//        purchasedLabel.isHidden = true
//      }
    } else {
      purchaseView.isHidden = false
      purchasedLabel.isHidden = true
    }
  }
  
  // MARK: - Public vars
  
  var delegate: GroupPurchaseViewDelegate?
  
  // MARK: - Lifecycle
  
  override func awakeFromNib() {
    super.awakeFromNib()
  }
  
  // MARK: - IBActions
  
  @IBAction func displayInfo() {
    delegate?.displayPurchaseInfo()
  }
  
  @IBAction func buy() {
    delegate?.buySession()
  }
  
  // MARK: - Public methods
  
  func prepare(price: Int, purchased: Bool, allAlbumInGroupPurchased: Bool, purchasedNote: String?, isFinished: Bool) {
    self.isFinished = isFinished
    priceLabel.text = "\(price) ×"
    if let noteString = purchasedNote {
      purchasedLabel.text = noteString
    }
    setUI(purchased: purchased, allAlbumInGroupPurchased: allAlbumInGroupPurchased)
  }
}

extension GroupPurcaseView {
  static let defaultHeight: CGFloat = 84
}
